package org.gongliang.jsmile.authc.sys.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.gongliang.jsmile.authc.sys.dao.SysResourcesDao;
import org.gongliang.jsmile.authc.sys.entity.SysResources;
import org.gongliang.jsmile.common.base.CRUDService;
import org.gongliang.jsmile.common.enums.MENUS;
import org.gongliang.jsmile.common.kit.StrKit;
import org.gongliang.jsmile.common.kit.web.ShiroKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 
* @author 龚亮
* @version 1.0
* @date 2018年08月25日 04:57:24
*/
@Service
public class SysResourcesService extends CRUDService<SysResourcesDao, SysResources> {

	@Autowired
	private SysResourcesDao resourcesDao;

	/**
	 * 
	 * @Description 查询角色所拥有的权限
	 * @author 龚亮
	 * @param userId
	 *            用户id
	 * @param type
	 *            权限类型 01菜单 02权限 -1全部
	 * @return 权限集合
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public List<SysResources> getMenuByRoles(Map<String,Object> param) {
		return resourcesDao.getMenuByRoles(param);
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean saveBasePermission(Integer parentId, Integer level) {
		SysResources save = new SysResources();
		save.setName("添加");
		save.setCreateDate(new Date());
		save.setCreator(ShiroKit.getLoginUser().getId());
		save.setParentId(parentId);
		save.setLevel(level);
		save.setPermCode("add");
		save.setMenuType(MENUS.BUTTON.getValue());

		SysResources delete = new SysResources();
		delete.setName("删除");
		delete.setCreateDate(new Date());
		delete.setCreator(ShiroKit.getLoginUser().getId());
		delete.setParentId(parentId);
		delete.setLevel(level);
		delete.setPermCode("delete");
		delete.setMenuType(MENUS.BUTTON.getValue());

		SysResources update = new SysResources();
		update.setName("修改");
		update.setCreateDate(new Date());
		update.setCreator(ShiroKit.getLoginUser().getId());
		update.setParentId(parentId);
		update.setLevel(level);
		update.setPermCode("update");
		update.setMenuType(MENUS.BUTTON.getValue());


		int i = 0;
		i += resourcesDao.saveOne(save);
		i += resourcesDao.saveOne(update);
		i += resourcesDao.saveOne(delete);
		return retBool(i);
	}

	/**
	 * desc: 保存
	 * @param ids
	 * @return
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean saveORupdate(SysResources resources) {
		int i=0;
		if(null==resources.getParentId()) {
			resources.setLevel(1);
		}else {
			SysResources r=new SysResources();
			r.setId(resources.getParentId());
			resources.setLevel(baseDao.findById(r).getLevel()+1);
		}
		if(null==resources.getId()) {
			i+=baseDao.saveOne(resources);
		}else {
			i+=baseDao.updateById(resources);
		}
		return retBool(i);
	}

	/**
	 * desc:批量删除资源
	 * @param ids
	 * @return
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean deleteResources(String[] ids) {
		int i=0;
		if (ids != null && ids.length >0) {
	        for(String id: ids) {
	        	if(StrKit.notBlank(id)) {
		        	i+=baseDao.deleteByPid(Integer.parseInt(id));
	        	}
	        }
		}
		i+=baseDao.deleteByIds(ids);
		return retBool(i);
	}
}
